package secondBrush.doubles;

import java.util.Arrays;

public class E04Leetcode977 {
    public int[] sortedSquares(int[] nums) {
        int length = nums.length;
        int[] result = new int[length];
        for (int i = 0; i < length; i++) {
            result[i] = nums[i] * nums[i];
        }
        Arrays.sort(result);
        return result;


    }

    //双指针解法
    public int[] sortedSquares2(int[] nums) {
        int length = nums.length;
        int left = 0, right = length - 1;
        int[] result = new int[length];
        for (int i = length -1 ; i >= 0; i--) {
            if (nums[left] * nums[left] <= nums[right] * nums[right]) {
                result[i] = nums[right] * nums[right];
                right--;
            } else {
                result[i] = nums[left] * nums[left];
                left++;
            }

            if (left > right) break;

        }
        return result;


    }
}
